package kded.demo.botp;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.botp.runtime.ConvertOperationResult;
import kd.bos.entity.botp.runtime.PushArgs;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.servicehelper.botp.ConvertServiceHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 源单需要在审核通过后自动生成下游单据
 * 方法二：后台代码下推
 * caijiasheng
 */
public class Demo2ForAudit extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        e.getFieldKeys().add("kded_entryentity");
    }
    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        e.addValidator(new AbstractValidator(){
            @Override
            public void validate() {

            }
        });
    }

    /**
     * 自动下推服务
     * 申请单审核通过、自动下推生成销售单
     * 参考文章
     * https://vip.kingdee.com/article/188698425926736128
     * https://dev.kingdee.com/index/docsNew/8d91a0a4-afa9-48d6-8750-0e81a5f1e7b9
     *
     * 关注点：
     * 1、PushArgs 参数的构造
     * 2、构造需要下推的数据结构
     * @param e e
     */
    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        DynamicObject[] dataEntities = e.getDataEntities();

        //构造下推参数
        PushArgs pushArgs = new PushArgs();
        pushArgs.setAutoSave(true);
        pushArgs.setSourceEntityNumber("kded_apply");
        pushArgs.setTargetEntityNumber("kded_ad_reimb");
        List<ListSelectedRow> selectedRows = new ArrayList<>();
        for (DynamicObject entry :
                dataEntities) {
            // 获取单据体所选数据行，生成ListSelectRow集合

            Object billId = entry.getPkValue();
            DynamicObjectCollection rows = entry.getDynamicObjectCollection("kded_entryentity");
            //默认下推所有分录行数据
            for (DynamicObject row : rows) {
                if (row.getDataEntityState().getFromDatabase()) {
                    // 分录行已经保存
                    Object entryId = row.getPkValue();      // 单据体行内码
                    ListSelectedRow selectedRow = new ListSelectedRow(billId);
                    selectedRow.setEntryEntityKey("kded_entryentity");     // 设置需要下推的单据体名：按分录下推时必须设置
                    selectedRow.setEntryPrimaryKeyValue(entryId);       // 设置需要下推的单据体行内码：按分录下推时必须设置
                    selectedRows.add(selectedRow);      // 加入的已选行集合中
                }
            }
        }
        pushArgs.setSelectedRows(selectedRows);

        // 开始下推
        ConvertOperationResult pushResult = ConvertServiceHelper.push(pushArgs);
        // 显示下推结果，自动打开目标单
        if (!pushResult.isSuccess()) {
            // 下推失败，显示错误报告
            System.out.println(pushResult.getMessage());
        }
    }
}
